package com.sharekitchen.service.impl;

import com.sharekitchen.dto.DeviceDto;
import com.sharekitchen.entity.*;
import com.sharekitchen.mapper.DeviceMapper;
import com.sharekitchen.service.DeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class DeviceServiceImpl implements DeviceService {
    @Autowired
    private DeviceMapper deviceMapper;

    @Override
    public List<Device> getAllDev(Page page) {
        return deviceMapper.getAllDev((page.getCurrentPage() - 1) * page.getPageSize(), page.getPageSize());
    }

    @Override
    public List<DeviceImages> getAllDevImg() {
        return deviceMapper.getAllDevImg();
    }

    @Override
    public List<DeviceDetail> getAllDevDetail() {
        return deviceMapper.getAllDevDetail();
    }

    @Override
    public Device getDevById(String devId) {
        return deviceMapper.getDevById(devId);
    }

    @Override
    public DeviceImages getDevImgById(String devId) {
        return deviceMapper.getDevImgById(devId);
    }

    @Override
    public DeviceDetail getDevDetailById(String devId) {
        return deviceMapper.getDevDetailById(devId);
    }

    @Override
    public boolean addDevice(DeviceDto deviceDto) {
        return deviceMapper.addDevice(deviceDto) > 0;
    }

    @Override
    public boolean addDeviceImages(DeviceImages deviceDto) {
        return deviceMapper.addDeviceImages(deviceDto) > 0;
    }

    @Override
    public boolean addDeviceDetail(DeviceDetail deviceDto) {
        return deviceMapper.addDeviceDetail(deviceDto) > 0;
    }

    @Override
    public boolean deleteDevice(String devId) {
        return deviceMapper.deleteDevice(devId) > 0;
    }

    @Override
    public boolean deleteDeviceImages(String devId) {
        return deviceMapper.deleteDeviceImages(devId) > 0;
    }

    @Override
    public boolean deleteDeviceDetail(String devId) {
        return deviceMapper.deleteDeviceDetail(devId) > 0;
    }

    @Override
    public boolean updateDevice(DeviceDto deviceDto) {
        return deviceMapper.updateDevice(deviceDto);
    }

    @Override
    public boolean updateDeviceImages(DeviceImages deviceImages) {
        if (deviceMapper.deleteDeviceImages(deviceImages.getDevId()) > 0) {
            return deviceMapper.addDeviceImages(deviceImages) > 0;
        }
        return false;
    }

    @Override
    public boolean updateDeviceDetail(DeviceDetail deviceDetail) {
        log.info(deviceDetail.toString());
        return deviceMapper.updateDeviceDetail(deviceDetail);
    }

    @Override
    public int getTotalCount() {
        return deviceMapper.getTotalCount();
    }

    @Override
    public List<Long> getDeviceNum() {
        DeviceNum deviceNum = deviceMapper.getDeviceNum();
        List<Long> devNum = new ArrayList<>();
        devNum.add(deviceNum.getDevNum());
        devNum.add(deviceNum.getDevUsable());
        devNum.add(deviceNum.getDevUsing());
        return devNum;
    }

}
